package com.ruoyi.web.controller.websocket;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.system.domain.LiveRoom;
import com.ruoyi.system.domain.Myuser;
import com.ruoyi.system.mapper.LiveMapper;
import com.ruoyi.system.mapper.LiveRoomMapper;
import com.ruoyi.system.mapper.MyuserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/*网页直播*/
//客户端通过这个uri连接webSocket
@ServerEndpoint("/api/onlineServer/{id}")
@Component
@Slf4j
public class OnlineServer {//每个客户端访问onlineServer连接的时候，创建一个OnlineServer实例
    private Session session;//当前会话对象通过session服务器想客户端发送消息
    public static Vector<OnlineServer> clients=new Vector<OnlineServer>(); //建立静态集合来存储所有客户端实例
    private static  int onlineCount;
//    private LiveMapper liveMapper;

  @OnOpen
    public void onOpen(Session session , @PathParam("id") Long id) throws IOException {
        this.session = session;
//        liveMapper.selectLiveById(id).getUid().startsWith("v")&&liveMapper.selectLiveById(id).getUsername()!=null
       /* if(myuserMapper.selectMyuserByUserid(id).getMogul()==1&&myuserMapper.selectMyuserByUserid(id)!=null){
            clients.add(this);//将当前客户端加入客户端列表
            addOnlineCount();//在线人数加1
            log.info("当前在线人数为"+getOnlineCount());
            System.out.println( myuserMapper.selectMyuserByUserid(id).getUserName()+" 主播开播 新连接建立！主播uid:"+myuserMapper.selectMyuserByUserid(id).getUserid());
        }else if(myuserMapper.selectMyuserByUserid(id).getMogul()==0&&myuserMapper.selectMyuserByUserid(id)!=null){
            clients.add(this);//将当前客户端加入客户端列表
            addOnlineCount();//在线人数加1
            log.info("当前在线人数为"+getOnlineCount());
            System.out.println(myuserMapper.selectMyuserByUserid(id).getUserName() +"用户  新连接建立！用户uid:"+myuserMapper.selectMyuserByUserid(id).getUserid());
        }else {
            System.out.println("连接失败！");
        }*/


        /*
        * 测试*/
        clients.add(this);//将当前客户端加入客户端列表
        addOnlineCount();//在线人数加1
        log.info("当前在线人数为"+getOnlineCount());
        System.out.println( "用户  新连接建立！用户id:"+id);

       /* String arr[]  = session.getQueryString().split("&");
        Integer id=Integer.getInteger(arr[0].split("=")[1]);
        String uid=arr[1].split("=")[1];
        String name = arr[2].split("=")[1];
        if(uid.startsWith("v")&& liveMapper.selectLiveById(id).getUid().equals(uid)&&liveMapper.selectLiveById(id).getUsername().equals(name)) {
            clients.add(this);//将当前客户端加入客户端列表
            addOnlineCount();//在线人数加1
            log.info("当前在线人数为"+getOnlineCount());
            System.out.println(name +" 主播开播 新连接建立！主播id:"+uid);
        }else if(uid.startsWith("l")&&liveMapper.selectLiveById(id).getUid().equals(uid)&&liveMapper.selectLiveById(id).getUsername().equals(name)){
            clients.add(this);//将当前客户端加入客户端列表
            addOnlineCount();//在线人数加1
            log.info("当前在线人数为"+getOnlineCount());
            System.out.println(name +"用户  新连接建立！用户id:"+uid);
        }*/
    }
    @OnClose
    public void onClose( ){
        clients.remove(this);
        subOnlineCount();
        log.info("有一个连接关闭！当前在线人数为"+getOnlineCount());
        System.out.println(" 有一个连接断开！");
    }

    @OnMessage//当客户端向服务器发送消息时触发的事件
    public void onMessage(String message,Session session){
        //把消息广播出去，群发到其他客户端
        String  aa;
        aa=String.valueOf(onlineCount)+";"+message;
        for(OnlineServer client:clients){
            try {
                //往个个客户端发送消息
                //client.session.getBasicRemote().sendText(aa);
                client.session.getBasicRemote().sendText(aa);
            } catch (IOException e) {
                //e.printStackTrace();
            }
        }
    }
    public static synchronized int getOnlineCount() {
        return onlineCount;
    }
    public static synchronized void addOnlineCount() {
        OnlineServer.onlineCount++;
    }
    public static synchronized void subOnlineCount() {
        OnlineServer.onlineCount--;
    }
}
